<?php
/**
 * Description of Questionnaireclass
 *
 * @author Chisheng Lu
 * @copyright DoRISE team, Science Education center, NTNU.
 * @since 2010-05-03
 * @version 1.0
 */
class Questionnaire {
    //put your code here
    private $mysqli;

    function __construct() {
        //引入資料庫連結參數設定
        include '../inc/oaConn.inc.php';
        $this->mysqli = @new mysqli($mysqlHost,$mysqlUser,$mysqlPassword,$mysqlDB);
        if(mysqli_connect_errno()) {
            throw new Exception('無法建立資料庫連線！'.mysqli_connect_error());
            $this->mysqli = false;
            exit();
        }

        //連線使用編碼
        $this->mysqli->query("SET NAMES 'utf8'");
    }

    /**
     *
     * @param Integer $intUserId
     * @return Integer
     */
    public function addQuestionnaire($intUserId) {
        $bolRtn = 0;
        $strNow = $this->getNowTime();
        $sql_addQn = 'INSERT INTO `Questionnaire` (`UserId`, `QnCreated`) ';
        $sql_addQn .= ' VALUES(' . $intUserId . ', ' . '\'' . $strNow . '\' );';
        
        $rs_addQn = $this->mysqli->query($sql_addQn);
        if (!$rs_addQn) {
            throw new Exception('新增問卷時發生錯誤！');
        }else{
            $bolRtn = $this->mysqli->insert_id;
        } 
        return $bolRtn;
    }

    /**
     *
     * @param Integer $intQnId
     * @param Integer $intUserId
     * @return Boolean
     */
    public function editQuestionnaire($intQnId, $intUserId) {
        //檢查問卷編號是否為空白
        if (empty ($intQnId)) {
            throw new Exception('缺少問卷編號！');
        }
        $bolRtn = false;
        $strNow = $this->getNowTime();
        $sql_editQn = 'Update Questionnaire set UserId= ' . $intUserId . ' ';
        $sql_editQn .= 'where QnId = ' . $intQnId . ';';
        //echo "sql_editQn:$sql_editQn<br>";
        $rs_editQn = $this->mysqli->query($sql_editQn);
        if (!$rs_editQn) {
            throw new Exception('更新問卷時發生錯誤！');
        }else{
            $bolRtn = true;
        }
        return $bolRtn;
    }

    /**
     *
     * @param Integer $intQnId
     * @return Boolean
     */
    public function delQuestionnaire($intQnId) {
        $bolRtn = false;
        //檢查問卷編號是否為空白
        if (empty ($intQnId)) {
            throw new Exception('錯誤！缺少問卷編號。');
        }

        //刪除
        include 'QnContent.Class.php';
        $objQnContent = new QnContent();
        $bolRtn = $objQnContent->delQnContentByQnId($intQnId);
        if (!$bolRtn) {
            throw new Exception('錯誤！刪除問卷內容時發生錯誤。');
        }
        $objQnContent = null;
        $sql_delQn = 'DELETE FROM `Questionnaire` WHERE `QnId`=' . $intQnId;
        $rs_delQn = $this->mysqli->query($sql_delQn);
        if (!$rs_delQn) {
            throw new Exception('錯誤！刪除問卷時發生錯誤。');
        }else{
            $bolRtn = true;
        }
        return $bolRtn;
    }

    /**
     *
     * @param Integer $intQnId
     * @return Array
     */
    public function getQuestionnaireInfoByQnId($intQnId) {
        //檢查問卷編號是否為空白
        if (empty ($intQnId)) {
            throw new Exception('缺少問卷編號！');
        }

        $sql_getQnById = 'SELECT * FROM `Questionnaire` WHERE `QnId`=' . $intQnId;
        $rs_getQnById = $this->mysqli->query($sql_getQnById);

        if (!$rs_getQnById) {
            throw new Exception('無法查詢該問卷的資料！');
        }else{
            $row_getQnById = $rs_getQnById->fetch_row();
            return $row_getQnById;
        } //End If
    }

    /**
     *
     * @param Integer $intQnId
     * @return Boolean: True: Locked, False: unLocked
     */
    public function chkExamLockByInQnId($intQnId) {
        //檢查試卷問卷編號是否為空白
        if (empty ($intQnId)) {
            throw new Exception('缺少該試卷的問卷編號！');
        }

        include 'ExamProcess.Class.php';

        $sql_getEBById = 'SELECT `ExamBasic`.`EBLock` FROM `ExamBasic`
            INNER JOIN `ExamProcess` ON `ExamProcess`.`EBId`= `ExamBasic`.`EBId`
            WHERE `ExamProcess`.`EPQEId`=' . $intQnId . ' AND `ExamProcess`.`EPCategory` = ' . ExamProcess::EPQuestionnaire;
        $rs_getEBById = $this->mysqli->query($sql_getEBById);

        if (!$rs_getEBById) {
            throw new Exception('無法查詢該問卷的試卷資料！');
        }else{
            if ($rs_getEBById->num_rows == 0) {
                throw new Exception('無此問卷的試卷資料！');
            }else{
                $row_getEBById = $rs_getEBById->fetch_row();
                return ($row_getEBById[0] == 1);
            }
        } //End If
    }

    /**
     *
     * @return Array
     */
    public function getQuestionnaireList() {
        $sql_getQnList = 'SELECT * FROM `Questionnaire` ;';
        $rs_getQnList = $this->mysqli->query($sql_getQnList);

        if (!$rs_getQnList) {
            throw new Exception('無法查詢該問卷的資料！');
        }else{
            $aryQnList = array();
            while ($aryOneQn = $rs_getQnList->fetch_row()) {
                array_push($aryQnList, $aryOneQn);
            }
            return $aryQnList;
        } //End If
    }

    /**
     *
     * @return String
     */
    private function getNowTime() {
        return date('Y-m-d H:i:s');
    }

    function  __destruct() {
        if ($this->mysqli) {
            $this->mysqli->close();
            $this->mysqli = FALSE;
        }
    }
}
?>
